package com.ruoyi.neusoft.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.neusoft.domain.*;
import com.ruoyi.neusoft.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.neusoft.service.IAiIdentifyModelTService;
import com.ruoyi.common.core.text.Convert;

/**
 * 智能AI识别模型Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@Service
public class AiIdentifyModelTServiceImpl implements IAiIdentifyModelTService {
    @Autowired
    private AiIdentifyModelTMapper aiIdentifyModelTMapper;

    // 模型校验
    @Autowired
    private AiIdentifyModelCheckTMapper checkTMapper;

    // 模型更新
    @Autowired
    private AiIdentifyModelVersionTMapper versionTMapper;

    // 模型训练
    @Autowired
    private AiIdentifyModelClassifyTMapper classifyTMapper;


    // AI智能识别数据集对象
    @Autowired
    private AiIdentifyCollectionTMapper collectionTMapper;


    /**
     * 查询智能AI识别模型
     *
     * @param modelId 智能AI识别模型主键
     * @return 智能AI识别模型
     */
    @Override
    public AiIdentifyModelT selectAiIdentifyModelTByModelId(Long modelId) {
        return aiIdentifyModelTMapper.selectAiIdentifyModelTByModelId(modelId);
    }

    /**
     * 查询智能AI识别模型列表
     *
     * @param aiIdentifyModelT 智能AI识别模型
     * @return 智能AI识别模型
     */
    @Override
    public List<AiIdentifyModelT> selectAiIdentifyModelTList(AiIdentifyModelT aiIdentifyModelT) {
        return aiIdentifyModelTMapper.selectAiIdentifyModelTList(aiIdentifyModelT);
    }

    /**
     * 新增智能AI识别模型
     *
     * @param aiIdentifyModelT 智能AI识别模型
     * @return 结果
     */
    @Override
    public int insertAiIdentifyModelT(AiIdentifyModelT aiIdentifyModelT) {
        aiIdentifyModelT.setCreateTime(DateUtils.getNowDate());
        //  新增

        int i = aiIdentifyModelTMapper.insertAiIdentifyModelT(aiIdentifyModelT);
        doAllInsert(aiIdentifyModelT);
        return i;
    }

    /**
     * 修改智能AI识别模型
     *
     * @param aiIdentifyModelT 智能AI识别模型
     * @return 结果
     */
    @Override
    public int updateAiIdentifyModelT(AiIdentifyModelT aiIdentifyModelT) {

        aiIdentifyModelT.setUpdateTime(DateUtils.getNowDate());
        //   修改
        int i = aiIdentifyModelTMapper.updateAiIdentifyModelT(aiIdentifyModelT);
        doAllUpdate(aiIdentifyModelT);
        return i;
    }

    /**
     * 批量删除智能AI识别模型
     *
     * @param modelIds 需要删除的智能AI识别模型主键
     * @return 结果
     */
    @Override
    public int deleteAiIdentifyModelTByModelIds(String modelIds) {
        //  删除
        String[] ids = Convert.toStrArray(modelIds);
        int i = aiIdentifyModelTMapper.deleteAiIdentifyModelTByModelIds(ids);
        doDelete(ids);
        return i;
    }

    /**
     * 删除智能AI识别模型信息
     *
     * @param modelId 智能AI识别模型主键
     * @return 结果
     */
    @Override
    public int deleteAiIdentifyModelTByModelId(Long modelId) {
        return aiIdentifyModelTMapper.deleteAiIdentifyModelTByModelId(modelId);
    }


    private void doAllInsert(AiIdentifyModelT t) {
        // 模型训练
        AiIdentifyModelClassifyT classifyT = new AiIdentifyModelClassifyT();
        BeanUtils.copyProperties(t, classifyT);
        classifyTMapper.insertAiIdentifyModelClassifyT(classifyT);

        // 模型更新
        AiIdentifyModelVersionT versionT = new AiIdentifyModelVersionT();
        BeanUtils.copyProperties(t, versionT);
        versionTMapper.insertAiIdentifyModelVersionT(versionT);

        // 模型校验
        AiIdentifyModelCheckT checkT = new AiIdentifyModelCheckT();
        BeanUtils.copyProperties(t, checkT);
        checkTMapper.insertAiIdentifyModelCheckT(checkT);

        // AI智能识别数据集对象
        AiIdentifyCollectionT collectionT = new AiIdentifyCollectionT();
        BeanUtils.copyProperties(t, collectionT);
        collectionTMapper.insertAiIdentifyCollectionT(collectionT);
    }

    private void doAllUpdate(AiIdentifyModelT t) {
        String tableName = "";
        Long modelId = t.getModelId();
        String modelName = t.getModelName();
        String modelKind = t.getModelKind();
        String modelCategory = modelKind;
        String sql = "";

        // 模型训练
        tableName = "ai_identify_model_classify_t";
        sql = "update ai_identify_model_classify_t set model_name = '"+modelName+"' ,model_category ='"+modelCategory+"' where model_id ="+modelId;
        aiIdentifyModelTMapper.updateAllCategory(sql);

        // 模型更新
        tableName = "";
        sql = "update ai_identify_model_version_t set model_name = '"+modelName+"' ,model_kind ='"+modelKind+"' where model_id ="+modelId;
        aiIdentifyModelTMapper.updateAllCategory(sql);

        // 模型校验
        tableName = "ai_identify_model_check_t";
        sql = "update ai_identify_model_check_t set model_name = '"+modelName+"' ,model_kind ='"+modelKind+"' where model_id ="+modelId;
        aiIdentifyModelTMapper.updateAllCategory(sql);

        // AI智能识别数据集对象
        tableName = "ai_identify_collection_t";
        sql = "update ai_identify_collection_t set model_name = '"+modelName+"' ,model_kind ='"+modelKind+"' where model_id ="+modelId;
        aiIdentifyModelTMapper.updateAllCategory(sql);
    }

    private void doDelete(String[] ids) {
        String tableName = "";
        String sql ="";


        // 模型训练
        tableName = "ai_identify_model_classify_t";
        aiIdentifyModelTMapper.deleteAllOne(ids);

        // 模型更新
        tableName = "ai_identify_model_version_t";
        aiIdentifyModelTMapper.deleteAllTwo(ids);

        // 模型校验
        tableName = "ai_identify_model_check_t";
        aiIdentifyModelTMapper.deleteAllThree(ids);

        // AI智能识别数据集对象
        tableName = "ai_identify_collection_t";
        aiIdentifyModelTMapper.deleteAllFour(ids);
    }
}
